<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <!-- <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="45%" v-dialogDrag append-to-body> -->
    <div class="add_wrap">
      <div class="title_add">{{title}}</div>
      <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      v-loading="formLoading"
      label-width="100px"
    >
      <el-form-item label="结果名" prop="name">
        <el-input v-model="formData.name" style="width:400px;" placeholder="请输入结果名" />
      </el-form-item>
      <el-form-item label="原始名" prop="oriName">
        <el-input v-model="formData.oriName" style="width:400px;" placeholder="请输入原始名" />
      </el-form-item>
      <el-form-item label="原始文件url" prop="oriUrl">
        <el-input v-model="formData.oriUrl" style="width:400px;" placeholder="请输入原始文件url" />
      </el-form-item>
    </el-form>
    <div>
      <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
      <el-button @click="reset">取 消</el-button>
    </div>
    <!-- </el-dialog> -->
  </div>
  </div>
</template>

<script>
import * as ResultApi from "@/api/ocr/result";
export default {
  name: "ResultForm",
  components: {},
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        id: undefined,
        name: undefined,
        oriName: undefined,
        oriUrl: undefined
      },
      // 表单校验
      formRules: {},
      title:"新增ocr结果"
    };
  },
  mounted(){
    let id=this.$route.query.id;
    this.open(id)
  },
  methods: {
    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      this.reset();
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          const res = await ResultApi.getResult(id);
          this.formData = res.data;
          this.title = "修改ocr结果";
        } finally {
          this.formLoading = false;
        }
      }else{
        this.formLoading = false;
        this.title = "新增ocr结果";
      }
      
    },
    /** 提交按钮 */
    async submitForm() {
      // 校验主表
      await this.$refs["formRef"].validate();
      this.formLoading = true;
      try {
        const data = this.formData;
        // 修改的提交
        if (data.id) {
          await ResultApi.updateResult(data);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          //this.$emit("success");
          this.reset();
          return;
        }
        // 添加的提交
        await ResultApi.createResult(data);
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.reset();
        //this.$emit("success");
      } finally {
        this.formLoading = false;
      }
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        id: undefined,
        name: undefined,
        oriName: undefined,
        oriUrl: undefined
      };
      this.resetForm("formRef");
    }
  }
};
</script>
<style>
.add_wrap{
  display: flex;
  justify-content: center;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.title_add{
  font-size: 20px;
  margin: 30px 0;
}
</style>
